package com.clkj.cldisabledmanclient.mvp.myenvalue;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyEnvalueBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MyEnvalueActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, MyEnvalueContract.View {

    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private PullToRefreshListView mLv;
    private RelativeLayout mRlEmpty;
    private TextView mTvTipEmpty;

    private ProgressDialog progressDialog;
    private List<MyEnvalueBean> myEnvalueBeanList = new ArrayList<>();
    private JayLvBaseAdapter<MyEnvalueBean> mAdapter;

    /**
     * 刷新标识
     */
    boolean isRefresh = false;
    /**
     * 是否可以加载更多标识
     */
    boolean canLoad = false;
    private Integer mPageIndex = 1;

    private String idCard;

    private MyEnvalueContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_envalue);
        initTitle(null, null, "我的评估", true, null);
        initData();
        initView();
        assignView();

        getMyEnvaluePlanList();
    }

    @Override
    public void initData() {

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new MyEnvaluePresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {
        mAdapter = new JayLvBaseAdapter<MyEnvalueBean>(this, myEnvalueBeanList, R.layout.item_my_envalue) {
            @Override
            public void convert(ViewHolder helper, int position, MyEnvalueBean item) {

                helper.setText(R.id.tv_assesstime,"评估时间："+item.getAssessmentTime());
                helper.setText(R.id.tv_assessteacher,"评估人："+item.getTeacherName());
            }
        };
        mLv.setAdapter(mAdapter);

        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent toDetail = new Intent(MyEnvalueActivity.this, MyEnvalueDetailActivity.class);
                toDetail.putExtra("assessmentId", myEnvalueBeanList.get(position - 1).getAssessmentId());
                startActivity(toDetail);
            }
        });
    }

    @Override
    public void initView() {

        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mLv = findViewById(R.id.lv);
        mRlEmpty = findViewById(R.id.rl_empty);
        mTvTipEmpty = findViewById(R.id.tv_tip_empty);

        mLv.setOnRefreshListener(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex = 1;
        isRefresh = true;
        getMyEnvaluePlanList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getMyEnvaluePlanList();
    }

    @Override
    public void getMyEnvaluePlanList() {

        mPresenter.getMyEnvaluePlanList(idCard, String.valueOf(mPageIndex), Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getMyEnvaluePlanListSuccess(List<MyEnvalueBean> myEnvalueBeans) {

        mLv.onRefreshComplete();

        if (isRefresh) {
            myEnvalueBeanList.clear();
            isRefresh = false;
        }

        if (myEnvalueBeans.size() > 0) {

            if (myEnvalueBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                mLv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            myEnvalueBeanList.addAll(myEnvalueBeans);

        } else {
            canLoad = false;
            mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (myEnvalueBeanList.size() == 0) {
                mLv.setEmptyView(mRlEmpty);
                mTvTipEmpty.setText("未查询到您的评估数据");
            } else {
                ToastUtil.showShort(this, TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMyEnvaluePlanListError(String tip) {

        mLv.onRefreshComplete();
        if (isRefresh) {
            myEnvalueBeanList.clear();
            isRefresh = false;
        }
        ToastUtil.showShort(this, tip);
        canLoad = false;
        mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        if (myEnvalueBeanList.size() == 0) {
            mLv.setEmptyView(mRlEmpty);
        }
    }

    @Override
    public void setPresenter(MyEnvalueContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
